﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Entity;
using PlanoAula.BD;
using System.Data.Entity.Infrastructure;
using System.Xml;
using System.Data.Objects;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace PlanoAula
{
    public partial class dbDataContext : DbContext
    {
        public DbSet<DiaNaoUtil> DiaNaoUtil { get; set; }
        public DbSet<DiaAula> DiaAula { get; set; }
        public DbSet<Avaliacao> Avaliacao { get; set; }
        public DbSet<PlanoAulaCab> PlanoAulaCab { get; set; }
        public DbSet<PlanoAulaDetalhe> PlanoAulaDetalhe { get; set; }
        public DbSet<UnidadeAvaliacao> UnidadeAvaliacao { get; set; }
        public DbSet<Universidade> Universidade { get; set; }
        public DbSet<Imagem> UniversidadeImage { get; set; }


        protected override bool ShouldValidateEntity(System.Data.Entity.Infrastructure.DbEntityEntry entityEntry)
        {
            
            // Required to prevent bug - http://stackoverflow.com/questions/5737733
            if (entityEntry.Entity is Universidade || entityEntry.Entity is Imagem)
            {
                return false;
            }
        
            
            return base.ShouldValidateEntity(entityEntry);
        }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingEntitySetNameConvention>();
            modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.OneToManyCascadeDeleteConvention>();
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            
            
            modelBuilder.Configurations.Add(new PlanoAulaCabConfiguration());
            modelBuilder.Configurations.Add(new PlanoAulaDetalheConfiguration());
            

            /*
            
            modelBuilder.Entity<Universidade>()
                        .HasRequired(e => e.UniversidadeImage)
                        .WithRequiredPrincipal();

            modelBuilder.Entity<Universidade>().ToTable("Universidade");
            modelBuilder.Entity<Imagem>().ToTable("Universidade");
            */
        }

        /// <summary>
        /// <see cref="http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx"/>
        /// </summary>
        /// <param name="context"></param>
        protected void Seed(DbContext context)
        {
            context.Database.ExecuteSqlCommand("ALTER TABLE Universidade ADD CONSTRAINT uc_Universidade UNIQUE(Nome)");
            //context.Database.ExecuteSqlCommand("ALTER TABLE Users ADD CONSTRAINT uc_Delivery UNIQUE(DeliveryAddressId)");
        }
        /// <summary>
        /// <see cref="http://stackoverflow.com/questions/4413084/unique-constraint-in-entity-framework-code-first"/>
        /// <seealso cref="http://romiller.com/2010/07/31/ef-ctp4-tips-tricks-running-additional-ddl/"/>
        /// <seealso cref="http://weblogs.asp.net/manavi/archive/2011/01/23/associations-in-ef-code-first-ctp5-part-3-one-to-one-foreign-key-associations.aspx"/>
        /// </summary>
        public class Initializer : IDatabaseInitializer<dbDataContext>
        {
            public void InitializeDatabase(dbDataContext context)
            {
                if (!context.Database.Exists() || !context.Database.CompatibleWithModel(false))
                {
                    context.Database.Delete();
                    context.Database.Create();

                    //context.ExecuteStoreCommand("CREATE UNIQUE CONSTRAINT...");
                    //context.ObjectContext.ExecuteStoreCommand("CREATE INDEX...");
                    //context.ObjectContext.ExecuteStoreCommand("ETC...");
                }
            }
        }
        public static string CreateDatabaseScript()
        {
            using (var context = new dbDataContext())
            {
                return ((IObjectContextAdapter)context).ObjectContext.CreateDatabaseScript();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="FilePath">Path do arquivo, ex. @"Model.edmx"</param>
        public static void GenerateRuntimeEDM(string FilePath)
        {
            using (var context = new dbDataContext())
            {
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;

                using (XmlWriter writer = XmlWriter.Create(FilePath, settings))
                {
                    EdmxWriter.WriteEdmx(context, writer);
                }
            }
        }
    }
}
